<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yxw/model')->方法名();
*/
namespace jy_yxw;
defined('BY_JYA') or exit('error');
class member extends \plugin{
	/*
		add
	*/
	public function add($postdata,$storeid=0,$noexi=false,$canadd=false){
		if(!$postdata['title']){
			if($noexi){
				return false;
			}
			exi('请填写客户名称！','error');
		}
		if(!$postdata['mobile']){
			if($noexi){
				return false;
			}
			exi('请填写客户手机号！','error');
		}
		if(!$postdata['from_type']){
			if($noexi){
				//return false;
			}
			//exi('请选择客户来源渠道！','error');
		}
		//成交/定金重复
		$isadd=pdo_get('jy_yxw_member',array('status between'=>array(10,20),'uniacid'=>$_SESSION['uniacid'],'mobile'=>$postdata['mobile']),array('id','title'));
		if($isadd['id']){
			$checkmsg=$postdata['title'].'-该客户已成交或已交定金！';
		}
		if(!$isadd['id']){
			//以“城市”为单位，同一个城市的门店的实体资源客户重复不可导入（无时间限制，永久不重复）
			$store=pdo_get('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$storeid?:$_SESSION['storeid']),array('id','pcd'));
			$pcd=json_decode($store['pcd'],true);
			$city=$pcd['city'];
			$storeids2=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%"'.$city.'%'),array('id'),'id');
			if($storeids2){
				$storeids2=array_keys($storeids2);
			}else{
				$storeids2=$storeid?:$_SESSION['storeid'];
			}
			$isadd=pdo_get('jy_yxw_member',array('storeid'=>$storeids2,'uniacid'=>$_SESSION['uniacid'],'mobile'=>$postdata['mobile']),array('id','title'));
			if($isadd['id']){
				$checkmsg=$postdata['title'].'-同一个城市的门店信息重复！';
			}
		}
		if(!$isadd['id']){
			//只要这个口碑录进了这个系统，那么全国的门店都录不进去这个人
			$isadd=pdo_get('jy_yxw_member',array('koubei_storeid >'=>0,'uniacid'=>$_SESSION['uniacid'],'mobile'=>$postdata['mobile']),array('id','title'));
			if($isadd['id']){
				$checkmsg=$postdata['title'].'-该客户已口碑录入！';
			}
		}
		if(!$canadd && $checkmsg && $isadd['id']!=$postdata['id']){
			if($noexi){
				return $postdata['id'];
			}
			exi($checkmsg,'error');
		}
		if(is_array($postdata['from_yonghuid'])){
			$from_yonghuid=$postdata['from_yonghuid']['id'];
		}
		$data=array(
			'title'=>trim($postdata['title']),
			'logo'=>$postdata['logo'],
			'mobile'=>trim($postdata['mobile']),
			'mobile2'=>trim($postdata['mobile2']),
			'wechat'=>trim($postdata['wechat']),
			'email'=>trim($postdata['email']),
			'qq'=>$postdata['qq'],
			'zhuanye'=>$postdata['zhuanye'],
			'workdesc'=>$postdata['workdesc'],
			'gangwei'=>$postdata['gangwei'],
			'school'=>$postdata['school'],
			'xueli'=>$postdata['xueli'],
			'sex'=>intval($postdata['sex']),
			'from_type'=>intval($postdata['from_type']),
			'from1_type'=>intval($postdata['from1_type']),
			'from2_type'=>intval($postdata['from2_type']),
			'from_yonghuid'=>$from_yonghuid,
			'from_workerid'=>intval($postdata['from_workerid']),
			'birth'=>$postdata['birth'],
			'pcd'=>ijson_encode($postdata['pcd']),
			'address'=>$postdata['address'],
			'cardno'=>$postdata['cardno'],
			'cardimg1'=>$postdata['cardimg1'],
			'cardimg2'=>$postdata['cardimg2'],
			'jj_mobile'=>$postdata['jj_mobile'],
			'jj_guanxi'=>$postdata['jj_guanxi'],
			'xueliimg'=>$postdata['xueliimg'],
			'bankno'=>$postdata['bankno'],
			'banktitle'=>$postdata['banktitle'],
			'bankimg'=>$postdata['bankimg'],
			'worker5id'=>$postdata['worker5id'],
			'worker6id'=>$postdata['worker6id'],
		);
		if($item['from_type']==4 && $item['from_workerid']){
			$data['workerheid']=$item['from_workerid'];
		}
		if($item['from_type']==3){
			$data['workerheid']=0;
		}
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_yxw_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			if($_SESSION['juese_id']==3){
				$data['worker3id']=$_SESSION['workerid'];
			}
			if($_SESSION['juese_id']==4){
				$data['worker4id']=$_SESSION['workerid'];
			}
			if($_SESSION['juese_id']==6){
				$data['worker6id']=$_SESSION['workerid'];
			}
			if($data['worker5id']){
				$data['status']=1;
			}
			if($data['worker6id']){
				$data['status']=32;
				$data['zhun_time']=time();
			}
			$data['koubei_storeid']=$postdata['koubei_storeid'];
			$storeid=$storeid?:$postdata['storeid'];
			$data['storeid']=$storeid?:$_SESSION['storeid'];
			if($_SESSION['juese_id']==3 || $_SESSION['juese_id']==4){
				$data['worker'.$_SESSION['juese_id'].'id']=$_SESSION['workerid'];
			}
			$data['createtime']=time();
			$data['up_time']=time();
			pdo_insert('jy_yxw_member',$data);
			$data['id']=pdo_insertid();
			$this->model('uplog')->upworkerlog($data,0);
			if($data['worker6id']){
				$this->model('uplog')->upworkerlog($data,32);
			}
		}
		return $id;
	}
	/*	
	*/
	public function edit_item($from_type=0,$settype=0){
		$item=array(
			array('type'=>'string','title'=>'客户名称','name'=>'title'),
			array('type'=>'string_two','title'=>'联系电话','title1'=>'手机号','title2'=>'备用手机号','name1'=>'mobile','name2'=>'mobile2'),
			array('type'=>'string_two','title'=>'紧急联系人','title1'=>'电话','title2'=>'关系','name1'=>'jj_mobile','name2'=>'jj_guanxi'),
			array('name'=>'from_type','title'=>'渠道','type'=>'radio','options'=>'from_type'),
			array('name'=>'from1_type','title'=>'线下来源渠道','type'=>'radio','options'=>'member_from1_type','show'=>array('from_type'=>1)),
			array('name'=>'from2_type','title'=>'网络来源渠道','type'=>'radio','options'=>'member_from2_type','show'=>array('from_type'=>2)),
			array('type'=>'string_two','title'=>'联系信息','title1'=>'微信号','title2'=>'QQ','name1'=>'wechat','name2'=>'qq'),
			array('type'=>'string','title'=>'邮箱','name'=>'email'),
			array('type'=>'radio','title'=>'性别','name'=>'sex','options'=>'sex'),
			array('type'=>'string_two','title'=>'年龄','title1'=>'出生年分','title2'=>'年龄','name1'=>'birth','name2'=>'age','read2'=>true),
			array('type'=>'pcd','title'=>'所在地','name'=>'pcd','help'=>''),
		);
		if($settype==5){
			unset($item[3],$item[4],$item[5]);
		}
		if($from_type){
			$item[4]=array('name'=>'from_yonghuid','title'=>'选择推荐的售后','type'=>'choose_titleup','action'=>'jy_yxw/get_yonghu/many/','show'=>array('from_type'=>3));
			$item[5]=array('type'=>'select','title'=>'推荐的员工','name'=>'from_workerid','options'=>'workers','show'=>array('from_type'=>4));
		}
		return $item;
	}
	public function edit_iteminfo($from_type=0,$settype=0){
		$item=array(
			array('type'=>'copy','title'=>'客户名称','name'=>'title'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array('type'=>'string','title'=>'邮箱','name'=>'email'),
			array('type'=>'string','title'=>'QQ','name'=>'qq'),
			array('type'=>'string','title'=>'专业','name'=>'zhuanye'),
			array('type'=>'string','title'=>'完结学校','name'=>'school'),
			array('type'=>'radio','title'=>'学历','name'=>'xueli','options'=>'xueli_type'),
			array('type'=>'image','title'=>'学信网/证书','name'=>'xueliimg','help'=>''),
			array('type'=>'string','title'=>'应聘岗位','name'=>'gangwei','show'=>array('from_type'=>1)),
			array('type'=>'string','title'=>'销售使用','name'=>'gangwei','show'=>array('from_type'=>2)),
			array('type'=>'string','title'=>'工作经验','name'=>'workdesc'),
			array('type'=>'radio','title'=>'性别','name'=>'sex','options'=>'sex'),
			array('type'=>'string_two','title'=>'年龄','title1'=>'出生年分','title2'=>'年龄','name1'=>'birth','name2'=>'age','read2'=>true),
			array('type'=>'pcd','title'=>'所在地','name'=>'pcd','help'=>''),
			array('type'=>'string','title'=>'身份证号码','name'=>'cardno','help'=>''),
			array('type'=>'image','title'=>'身份证正面','name'=>'cardimg1','help'=>''),
			array('type'=>'image','title'=>'身份证背面','name'=>'cardimg2','help'=>''),
			array('type'=>'string','title'=>'详细地址','name'=>'address','help'=>''),
			array('type'=>'string','title'=>'银行卡号','name'=>'bankno','help'=>''),
			array('type'=>'string','title'=>'银行支行','name'=>'banktitle','help'=>''),
			array('type'=>'image','title'=>'银行卡正面照片','name'=>'bankimg','help'=>''),
		);
		if($settype==5){
			unset($item[3],$item[4],$item[5]);
		}
		if($from_type){
			$item[4]=array('name'=>'from_yonghuid','title'=>'选择推荐的售后','type'=>'choose_titleup','action'=>'jy_yxw/get_yonghu/many/','show'=>array('from_type'=>3));
			$item[5]=array('type'=>'select','title'=>'推荐的员工','name'=>'from_workerid','options'=>'workers','show'=>array('from_type'=>4));
		}
		return $item;
	}
	public function daoru($post=array()){
		if(!$post['excel']){
			exi('请先上传CSV文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel($localexcel);
		$res=$this->model('model')->get_csvitems($excel);
		if(!$res[0]['电话'] && !$res[0]['姓名']){
			$excel=cfc('tools')->importExcel($localexcel,true);
			$res=$this->model('model')->get_csvitems($excel);
			if(!$res[0]['电话'] && !$res[0]['姓名']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res,$post['from_type']);
		if($cdata){
			cache_write('cdata',$cdata,'jy_yxw'.$_SESSION['workerid']);
			exi('导入成功（电话重复的会自动忽略）！','','/member.cdata/');
		}else{
			exi('导入成功！');
		}
	}
	public function csv_add($rows,$from_type=1){
		$cdata=array();
		$pcd=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['storeid']),'pcd');
		$pcd=json_decode($pcd,true);
		$city=$pcd['city'];
		$storeids=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%'.$city.'%'),array('id'),'id');
		if($storeids){
			$storeids=array_keys($storeids);
		}else{
			$storeids=$_SESSION['storeid'];
		}
		foreach($rows as $item){
			if(!$item['姓名']){
				//exi('有数据不存在姓名！','error');
			}
			$data=array(
				'title'=>trim($item['姓名']),
				'mobile'=>trim($item['电话']),
				'zhuanye'=>trim($item['专业']),
				'workdesc'=>trim($item['工作经验']),
				'gangwei'=>trim($item['应聘岗位']),
			);
			if($item['所属门店']){
				$store=pdo_get('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>trim($item['所属门店'])),array('id','pcd'));
				$data['storeid']=$store['id'];
				if(!in_array($data['storeid'],$storeids)){
					$pcd=json_decode($store['pcd'],true);
					$city=$pcd['city'];
					$storeids2=pdo_getall('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'pcd like'=>'%"'.$city.'%'),array('id'),'id');
					if($storeids2){
						$storeids2=array_keys($storeids2);
					}else{
						$storeids2=$data['storeid'];
					}
				}
			}
			if($item['电客人员']){
				$data['worker5id']=pdo_getcolumn('jy_yxw_worker',array('juese'=>5,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['电客人员'])),'id');
			}
			if($item['销售员']){
				$data['worker6id']=pdo_getcolumn('jy_yxw_worker',array('juese'=>6,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'title'=>trim($item['销售员'])),'id');
			}
			if($item['性别']){
				$item['性别']=trim($item['性别']);
				$data['sex']=$item['性别']=='男'?1:2;
			}
			if($item['年龄']){
				$age=intval($item['年龄']);
				$birth=date('Y');
				$birth -=$age;
				$data['birth']=$birth;
			}
			$xueli_type=$this->xueli_type;
			foreach($xueli_type as $k=>$xv){
				if(strpos($xv,$item['学历']) !== false){
					$data['xueli']=$k;
				}
			}
			$data['from_type']=$from_type;
			if($from_type!=2){
				$member_from1_type=$this->member_from1_type;
				foreach($member_from1_type as $k=>$mv){
					if(strpos($xv,$item['渠道来源']) !== false){
						$data['from1_type']=$k;
					}
					if(strpos($item['渠道来源'],'58') !== false){
						$data['from1_type']=2;
					}
					if(strpos($item['渠道来源'],'58下载') !== false){
						$data['from1_type']=1;
					}
					if(strpos($item['渠道来源'],'58主投') !== false){
						$data['from1_type']=2;
					}
					if(strpos($item['渠道来源'],'前程') !== false){
						$data['from1_type']=3;
					}
					if(strpos($item['渠道来源'],'智联') !== false){
						$data['from1_type']=4;
					}
					if(strpos($item['渠道来源'],'boss') !== false){
						$data['from1_type']=5;
					}
					if(strpos($item['渠道来源'],'口碑') !== false){
						$data['from1_type']=6;
					}
					if(strpos($item['渠道来源'],'直上') !== false){
						$data['from1_type']=7;
					}
				}
			}else{
					$member_from2_type=$this->member_from2_type;
					foreach($member_from2_type as $k=>$mv){
						if(strpos($xv,$item['渠道来源']) !== false){
							$data['from2_type']=$k;

						}
						if(strpos($item['渠道来源'],'360') !== false){
							$data['from2_type']=2;
						}
					}
			}
			//成交/定金重复
			$isadd=pdo_get('jy_yxw_member',array('status between'=>array(10,20),'uniacid'=>$_SESSION['uniacid'],'mobile'=>$data['mobile']),array('id','title'));
			if($isadd['id']){
				$cdata[]=array('title'=>$data['title'],'mobile'=>$data['mobile'],'type'=>1);
				continue;
			}
			//以“城市”为单位，同一个城市的门店的实体资源客户重复不可导入（无时间限制，永久不重复）
			$isadd=pdo_get('jy_yxw_member',array('storeid'=>$storeids2?:$storeids,'uniacid'=>$_SESSION['uniacid'],'mobile'=>$data['mobile']),array('id','title'));
			if($isadd['id']){
				$cdata[]=array('title'=>$data['title'],'mobile'=>$data['mobile'],'type'=>2);
				continue;
			}
			//只要这个口碑录进了这个系统，那么全国的门店都录不进去这个人
			$isadd=pdo_get('jy_yxw_member',array('koubei_storeid >'=>0,'uniacid'=>$_SESSION['uniacid'],'mobile'=>$postdata['mobile']),array('id','title'));
			if($isadd['id']){
				$cdata[]=array('title'=>$data['title'],'mobile'=>$data['mobile'],'type'=>3);
				continue;
			}
			$this->add($data,0,true,true);
		}
		return $cdata;
	}
	public function member_show($id,$default_url='',$default=false){		
		$member=pdo_get('jy_yxw_member',array('id'=>intval($id)),array('id','title','logo','from_type'));
		if(!$member){
			$member=$default?:array('nickname'=>'无');
		}else{
			$member['nickname']=$member['title']?:'无';
			$member['avatar']=tomedia($member['logo']);
		}
		$member['url']='jy_yxw/member.list/look/';
		return $member;
	}
	public function check_del($id=0,$from='',$juese=0){
		if(!$id){
			exi('请选择要删除的客户！','error');
		}
		$member=pdo_get('jy_yxw_member',array('id'=>intval($id)),array('id','title','mobile','logo','from_type','status'));
		if($from=='counts'){
			if($member['status']){
				exi('已分配客户，推广部不能删除！','error');
			}
		}
		if($from=='yonghu'){
			if($member['status']){
				exi('已分配客户，售后部不能删除！','error');
			}
		}
		if($from=='xiaoshou'){
			exi('销售部不能删除客户！','error');
		}
		if($juese==4 || $juese==5){
			if($member['status']){
				exi('已分配客户，推广部不能删除！','error');
			}
		}
		if($juese==4 || $juese==5){
			if($member['status']){
				exi('已分配客户，推广部不能删除！','error');
			}
		}
		return true;
	}
	public function up_haidata(){
		$basic=$this->plugin_setting('basic');
		if(intval($basic['sf_day'])<=0){
			return false;
		}
		$members=pdo_getall('jy_yxw_member',array('up_time'=>0,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','createtime'));
		foreach($members as $item){
			pdo_update('jy_yxw_member',array('up_time'=>$item['createtime']),array('id'=>$item['id'],'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
		}
		$up_time=time()-intval($basic['sf_day'])*24*3600;
		pdo_update('jy_yxw_member',array('status'=>100,'worker6id'=>0),array('jibie_type <>'=>5,'status'=>array(2,32),'up_time <'=>$up_time,'uniacid'=>$_SESSION['uniacid']));
		$basic['sf_dianday']=$basic['sf_dianday']?:$basic['sf_day'];
		$up_time=time()-intval($basic['sf_dianday'])*24*3600;
		pdo_update('jy_yxw_member',array('status'=>110,'worker5id'=>0),array('jibie_type <>'=>5,'status'=>array(1,2,3,30,31),'up_time <'=>$up_time,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function gestatus($juese=0){
		$juese=$juese?:$_SESSION['juese_id'];
		if(!$juese || $juese=='all'){
			$member_status=$this->member_status;
		}
		if($juese==6){
			$member_status=$this->member_status1;
			unset($member_status[0],$member_status[1],$member_status[2]);
		}
		if($juese==7){
			$member_status=$this->member_status1;
			unset($member_status[0],$member_status[1],$member_status[2],$member_status[3],$member_status[4]);
		}
		if($juese==8){
			$member_status=$this->member_status1;
			unset($member_status[0],$member_status[1],$member_status[2],$member_status[3],$member_status[4],$member_status[5],$member_status[6]);
			$member_status[11]=$this->member_status[11];
		}
		return $member_status;
	}
}
?>